package com.jsnu.xiaochi.controller;

import com.alibaba.fastjson.JSON;
import com.jsnu.xiaochi.service.IndexService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
public class IndexController {
    @Resource
    IndexService indexService;
    @RequestMapping("index")
    public String showIndex(Model model){
        model.addAttribute("banners",indexService.getBanner());
        model.addAttribute("deliciouses",indexService.findDelicious().getList());
        model.addAttribute("recommends",indexService.findRecommend().getList());
        return "index";
    }

    @RequestMapping("/")
    public String showIndex2(Model model){
        return  showIndex(model);
    }

    @RequestMapping("getAllFood")
    @ResponseBody
    public String getAllFood(){
        Map map= new HashMap<>();
        map.put("data",indexService.getAllFood());
        return JSON.toJSONString(map);
    }
}
